//
//  XLVistorView.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/23.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLVistorView: UIView {

    var closure:(() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        setCircleImageAnim()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 点击事件
    @objc private func loginClick(){
//        print("注册/登录 Btn被点击了")
        
        // 执行闭包
        closure?()
    }
    
    // 设置访客视图info
    func setVistorViewInfo(imgNamed: String?, title: String?){
        if let img = imgNamed, let tit = title{
            iconImage.image = UIImage(named: img)
            messageLabel.text = tit
            // 隐藏圆圈
            circleImageView.isHidden = true
        }else{
            setCircleImageAnim()
        }
        
        
    }
    
    // 设置圆圈动画
    private func setCircleImageAnim(){
        
        // 核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置旋转角度
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 20
        // 重复次数
        anim.repeatCount = MAXFLOAT
        
        anim.isRemovedOnCompletion = false
        
        // 添加动画
        circleImageView.layer.add(anim, forKey: nil)
        
    }
    
    
    // 创建视图
    private func setupUI(){
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImage)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        // 01 圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 02 遮罩
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 03 icon视图
        iconImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        // 04 文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(230)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        // 05 注册按钮
        registerBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        // 06 注册按钮
        loginBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.right.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }

        
        
    }
    
    // 懒加载视图 
    // 01 圆圈 图片
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imgNamed: "visitordiscover_feed_image_smallicon")
    // 02 遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imgNamed: "visitordiscover_feed_mask_smallicon")
    // 03 icon 视图
    fileprivate lazy var iconImage: UIImageView = UIImageView(imgNamed: "visitordiscover_feed_image_house")
    // 04 文字
    fileprivate lazy var messageLabel: UILabel = {
        let lab = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: XLNormalFontSize, textColor: UIColor.darkGray)
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
    // 05 注册按钮
    fileprivate lazy var registerBtn: UIButton = {
        let btn = UIButton(setBackgroundImageName: "common_button_white", title: "注册", fontSize: XLNormalFontSize, titleColor: XLTHENECOLOR, target: self, action: #selector(loginClick))
        return btn
    }()
    
    // 06 登录按钮
    fileprivate lazy var loginBtn: UIButton = {
        let btn = UIButton(setBackgroundImageName: "common_button_white", title: "登录", fontSize: XLNormalFontSize, titleColor: XLTHENECOLOR, target: self, action: #selector(loginClick))
        return btn
    }()
}












